package com.sz.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.file.service.UploadFileService;

import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.Equipment;
import com.sz.mes.dto.EquipmentDTO;
import com.sz.mes.vo.EquipmentListVO;
import com.sz.mes.vo.EquipmentDetailVO;
import com.sz.mes.service.EquipmentService;
import com.sz.mes.controller.BaseCustomCrudRestController;

import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import java.util.List;

/**
 * 设备 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2023-06-06
 * Copyright © szhs
 */
@RestController
@RequestMapping("/equipment")
@BindPermission(name = "设备")
@Slf4j
public class EquipmentController extends BaseCustomCrudRestController<Equipment> {
    @Autowired
    private UploadFileService uploadFileService;

    @Autowired
    private EquipmentService equipmentService;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
//    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(EquipmentDTO queryDto, Pagination pagination) throws Exception {
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryDto.getEquipmentNumber())) {
            equipmentQueryWrapper.like("equipment_number", queryDto.getEquipmentNumber());
        } else if (!StringUtils.isEmpty(queryDto.getEquipmentName())) {
            equipmentQueryWrapper.like("equipment_name", queryDto.getEquipmentName());
        } else {
            return super.getViewObjectList(queryDto, pagination, EquipmentListVO.class);
        }
        return super.getEntityListWithPaging(equipmentQueryWrapper, pagination);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
//    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, EquipmentDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
//    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody Equipment entity) throws Exception {
        return super.createEntity(entity);
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
//    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody Equipment entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
//    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
        return super.deleteEntity(id);
    }

    /**
     * 创建对象后操作
     *
     * @param entityOrDto
     * @throws Exception
     */
    @Override
    protected void afterCreated(Equipment entityOrDto) throws Exception {
        uploadFileService.bindRelObjId(entityOrDto.getPrimaryKeyVal(), entityOrDto.getClass(), entityOrDto.getFileUuidList());
    }

    /**
     * 更新对象后操作
     *
     * @param entityOrDto
     * @throws Exception
     */
    @Override
    protected void afterUpdated(Equipment entityOrDto) throws Exception {
        uploadFileService.bindRelObjId(entityOrDto.getPrimaryKeyVal(), entityOrDto.getClass(), entityOrDto.getFileUuidList());
    }


}
